package sketch.semantics.link;

import java.util.ArrayList;
import java.util.List;

import sketch.doc.Sketch;
import sketch.semantics.SGGenerator;
import sketch.semantics.SemanticGraph;
import sketch.shape.Shape;
import sketch.shape.domain.Link;
import sketch.shape.domain.LinkEnd;

public class LinkSGG extends SGGenerator {

	public void generateOn(SemanticGraph sg) {
		Sketch sketch = sg.getSketch();
		List<Shape> shapes = sketch.getShapes();
		List<Link> links = new ArrayList<Link>();

		for (Shape shape : shapes) {
			if (shape instanceof Link) {
				links.add((Link) shape);
			} else {
				// process boxes
				LinkBox box = new LinkBox();
				box.setShapeId(shape.getId());
				sg.addConcept(box);
			}
		}

		for (Link link : links) {
			// process links
			LinkEnd[] ends = new LinkEnd[link.getLinkEnds().size()];
			link.getLinkEnds().toArray(ends);

			for (int i = 0; i < ends.length; i++) {
				LinkEnd ii = ends[i];
				for (int j = i+1; j < ends.length; j++) {
					LinkEnd jj = ends[j];

					LinkConnection con = new LinkConnection();
					con.addOperant(sg.getConceptByShapeId(ii.getEndShapeId()));
					con.addOperant(sg.getConceptByShapeId(jj.getEndShapeId()));
					sg.addRelation(con);

				}
			}
		}
	}

}
